<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Runs JavaScript code on the database server.</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongodb.dropcollection.html">MongoDB::dropCollection</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.forceerror.html">MongoDB::forceError</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongodb.execute" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB::execute</h1>
  <p class="verinfo">(PECL mongo &gt;=0.9.3)</p><p class="refpurpose"><span class="refname">MongoDB::execute</span> &mdash; <span class="dc-title">Runs JavaScript code on the database server.</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb.execute-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span> <span class="methodname"><strong>MongoDB::execute</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$code</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$args</code><span class="initializer"> = array()</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   The Mongo database server runs a JavaScript engine.  This method allows you
   to run arbitary JavaScript on the database.  This can be useful if you want
   touch a number of collections lightly, or process some results on the
   database side to reduce the amount that has to be sent to the client.
  </p>
  <p class="para">
   Running JavaScript in the database takes a write lock, meaning it blocks 
   other operations.  Make sure you consider this before running a long script.
  </p>
  <p class="para">
   This is a wrapper for a database command.  This method is basically:
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$code</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$args</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">command</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'$eval'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$code</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'args'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$args</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </p>
  <p class="para">
   MongoDB implies a return statement if you have a single statement on a single
   line. This can cause some unintuitive behavior.  For example, this returns 
   &quot;foo&quot;:
  </p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'"foo";'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <p class="para">
   However, these return <strong><code>NULL</code></strong>:
  </p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'"bar";&nbsp;"foo";'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;more&nbsp;than&nbsp;one&nbsp;statement<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'db.foo.count(<br />);'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;more&nbsp;than&nbsp;one&nbsp;line<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <p class="para">
   To avoid surprising behavior, it is best not to depend on MongoDB to decide 
   what to return, but to explicitly state a return value. In the examples 
   above, we can change them to:
  </p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'"bar";&nbsp;return&nbsp;"foo";'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">'return&nbsp;db.foo.count(<br />);'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <p class="para">
   Now the first statement will return &quot;foo&quot; and the second statement will 
   return a count of the &quot;foo&quot; collection.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb.execute-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>

      <em><code class="parameter">code</code></em>
     </dt>

     <dd>

      <p class="para">
       <a href="class.mongocode.html" class="classname">MongoCode</a> or string to execute.
      </p>
     </dd>

       
    
     <dt>

      <em><code class="parameter">args</code></em>
     </dt>

     <dd>

      <p class="para">
       Arguments to be passed to <em>code</em>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodb.execute-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the result of the evaluation.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongodb.execute-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1389">
   <p><strong>Example #1 Simple <span class="function"><strong>MongoDB::execute()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$response&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">"function()&nbsp;{&nbsp;return&nbsp;'Hello,&nbsp;world!';&nbsp;}"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$response</span><span style="color: #007700">[</span><span style="color: #DD0000">'retval'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen"><br />
Hello, world!<br />
   </div>
  </div>
  <div class="example" id="example-1390">
   <p><strong>Example #2 Parameter <span class="function"><strong>MongoDB::execute()</strong></span> example</strong></p>
   <div class="example-contents"><p>
    The optional array of parameters will be passed to the JavaScript function.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$response&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #DD0000">"function(greeting,&nbsp;name)&nbsp;{&nbsp;return&nbsp;greeting+',&nbsp;'+name+'!';&nbsp;}"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"Good&nbsp;bye"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Joe"</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">$response</span><span style="color: #007700">[</span><span style="color: #DD0000">'retval'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen"><br />
Good bye, Joe!<br />
   </div>
  </div>
  <div class="example" id="example-1391">
   <p><strong>Example #3 Scope example</strong></p>
   <div class="example-contents"><p>
    If a <a href="class.mongocode.html" class="classname">MongoCode</a> object is used instead of a string for
    the first parameter, a scope can be passed in which the JavaScript will be 
    executed.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$func&nbsp;</span><span style="color: #007700">=&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"function(greeting,&nbsp;name)&nbsp;{&nbsp;"</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"return&nbsp;greeting+',&nbsp;'+name+',&nbsp;says&nbsp;'+greeter;"</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"}"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$scope&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"greeter"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Fred"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$code&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoCode</span><span style="color: #007700">(</span><span style="color: #0000BB">$func</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$scope</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$response&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$code</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"Goodbye"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Joe"</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">$response</span><span style="color: #007700">[</span><span style="color: #DD0000">'retval'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen"><br />
Goodbye, Joe, says Fred <br />
   </div>
  </div>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongodb.dropcollection.html">MongoDB::dropCollection</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.forceerror.html">MongoDB::forceError</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
